package com.hundsun.yr.universal.p2p.ui.activity;

import android.net.http.SslError;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.hundsun.mobile.zwgt.R;
import com.hundsun.yr.universal.library.base.BaseWebActivity;
import com.hundsun.yr.universal.library.netstatus.NetUtils;
import com.hundsun.yr.universal.p2p.config.Extras;


/**
 * @version V1.0
 * @Title: ProductDescriptionActivity.java
 * @Description: 产品详情
 * @date 2015-12-4 上午10:01:08
 * @author:hejun.shen
 * @email:shenhj15971@hundsun.com
 * @replace author:
 * @replace date:
 */
public class ProductDescriptionActivity extends BaseWebActivity implements View.OnClickListener {
    /**
     * 跳转标识
     */
    private String NAVIGATE_TAG;

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        // TODO Auto-generated method stub
        return true;
    }

    @Override
    protected View getLoadingTargetView() {

        if (mBrowserLayout == null) {
        }
        return mBrowserLayout;
    }


    @Override
    protected void onNetworkConnected(NetUtils.NetType type) {
        mBrowserLayout.refresh();
        toggleHide();
    }

    @Override
    protected void getBundleExtras(Bundle extras) {
        super.getBundleExtras(extras);
        NAVIGATE_TAG = extras.getString(Extras.NAVIGATE_TAG);
    }

    @Override
    protected void onNetworkDisConnected() {

        toggleNetworkError(true, new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                if (NetUtils.isNetworkConnected(getBaseContext())) {
                    showToast("请检查网络...");
                } else {
                    mBrowserLayout.refresh();
                    toggleHide();
                }
            }
        });

    }

    @Override
    protected void initViewsAndEvents() {
        super.initViewsAndEvents();
        toolbar_title.setText(mWebTitle);
//		toolbar_menu.setText(R.string.complete);
//		toolbar_menu.setTextColor(getResources().getColor(R.color.white));
//		toolbar_menu.setVisibility(View.GONE);
        toolbar_menu.setOnClickListener(this);
        mBrowserLayout.mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                String pay_callback = getString(R.string.pay_callback_url);
                String callback_unkown = getString(R.string.callback_unkown_url);
                if (url.contains(pay_callback)) {
                    toolbar_menu.setVisibility(View.VISIBLE);
                    mBrowserLayout.loadUrl(url.substring(0, url.indexOf(pay_callback) + pay_callback.length()));
                } else if (url.contains(callback_unkown)) {
                    toolbar_menu.setVisibility(View.VISIBLE);
                    mBrowserLayout.loadUrl(url.substring(0, url.indexOf(callback_unkown) + callback_unkown.length()));
                }
                return super.shouldOverrideUrlLoading(view, url);
            }
        });
    }

    @Override
    protected boolean isApplyKitKatTranslucency() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
            case R.id.toolbar_menu:
                if (NAVIGATE_TAG != null && TextUtils.equals(NAVIGATE_TAG, "TRADE")) {
                    finish();
                } else {
//				readyGoThenKill(MineInvestActivity.class);
//				readyGoThenKill(MainActivity.class);
                    finish();
                }
                break;
            default:
                break;
        }
    }
}
